
<link rel="stylesheet" href="<?= $this->config->base_url(); ?>assets/css/chosen.css" type="text/css" charset="utf-8" />



<script type="text/javascript">

$(document).ready(function(){
	$('#date').datepicker({ dateFormat: 'yy-mm-dd' });
	
})


$(function() {
        var scntDiv = $('#p_scents');
        var i = $('#p_scents tr').size() + 1;
     $('.addScnt').live('click', function() {
                $('<tr id="dd'+ i +'"> <td><input type="text" class="span8"  name="code[]" onchange="get_product1(this.value,rate'+ i +');"/></td> <td><input type="text"  class="span8" name="rate[]"   id="rate'+ i +'" onchange="count_all1(this.value, '+i+');"  /></td> <td><input type="text" class="span8" name="quantity[]" id="quantity'+ i +'" onblur="count_amount1(this.value, '+i+' );" /><input type="hidden" id="hid_qty'+ i +'" class="span8" ></td> <td><input type="text" class="span8" name="amount[]" readonly="readonly" id="amt'+ i +'" /><input type="hidden" id="hid_amt'+ i +'" class="span8" ></td> <td><input type="text" class="span8" name="discount[]" id="discount'+ i +'" onchange="count_distotal1(this.value, '+i+')"  /><input type="hidden" id="hid_disc'+ i +'" class="span8" ></td> <td><select id="tax'+ i +'" onchange="count_total1(this.value, '+i+');"  class="span12"  name="tax[]" class="chzn-select"> <option value="" >Select</option><?php foreach($taxes as $tax) {?><option value="<?= $tax->amount?>"<?php if(isset($edit)) {if($edit['project_id'] == $tax->amount){echo 'selected = "selected"';}}else {echo set_select('project',$tax->id);}?> ><?= $tax->name?></option><?php }?></select><input type="hidden" id="hid_tax'+ i +'" class="span8" ></td><td><input type="text" class="span12" name="total[]" readonly="readonly"  id="total'+ i +'"/><input type="hidden" class="span12"  id="hid_total'+ i +'"/><td><a href="#" class="addScnt">ADD</a>&nbsp;&nbsp;&nbsp;<a href="#" id="'+i+'" class="remScnt">Remove</a></td></tr>').appendTo(scntDiv);
                i++;
                return false;
        });
        
        $('.remScnt').live('click', function() { 
       		alert(this.id)
			var Y= parseInt(this.id)
		var t='#'+'amt'+Y
		var tx='#'+'hid_tax'+Y
		var to='#'+'total'+Y
		var ds='#'+'hid_disc'+Y
		var c_amt= parseFloat($(t).val())
		var c_disc = parseFloat($(ds).val())
		var c_tax= parseFloat($(tx).val())
		var c_total	= parseFloat($(to).val())
		var f_amt= parseFloat($('#f_amt').val())
		var f_disc = parseFloat($('#f_disc').val())
		var f_tax= parseFloat($('#f_tax').val())
		var f_total	= parseFloat($('#f_total').val())
		
		//alert(c_tax)
		if($(t).val()!=''){
			$('#f_amt').val((f_amt - c_amt).toFixed(2))			
		}else{
			$('#f_amt').val((f_amt).toFixed(2))
		}
		if($(ds).val()!=''){
			$('#f_disc').val((f_disc - c_disc).toFixed(2))			
		}else{
			$('#f_disc').val((f_disc).toFixed(2))
		}
		if($(tx).val()!=''){		
			$('#f_tax').val((f_tax - c_tax).toFixed(2))	
		}else{
			$('#f_tax').val((f_tax).toFixed(2))
		}
		if($(to).val()!=''){
				$('#f_total').val((f_total - c_total).toFixed(2))
		
		}else{
				$('#f_total').val((f_total).toFixed(2))
		}
	   
	            if( i > 2 ) {
                        
						$(this).parents('tr').remove();
                        i--;
                }
                return false;
        });
});




    


function get_product1(code,rate){
		var id="#"+$(rate).attr('id')
		$.post("<?= site_url('sales/sales/get_product')?>",{ code:code }, function(msg){
		if(msg!=1){
				$(id).val(msg);	

			}else{
				$(id).val(""),
				alert('No Product Is Available!!')
				}
		
		})
	
}	



var counter1=0
	function count_total(tax, dis, amt,excise){
		if(amt == ''){
		return false	
		}
		if(tax != ''){
		if(dis=='' || excise==''){
			if(dis == '' && excise != ''){
				var tax_per,disc_per;
			excise = parseFloat(excise)	
			p=parseFloat(amt)+ excise,
			t=parseFloat(tax),
			tax_per= (p*t)/100,
			to_tax= p+tax_per,
			disc_per= 0,
			final_amt=to_tax ,
			counter1=counter1+1
			if(counter1 >1){
		
				var f_tax = parseFloat($("#f_tax").val())
				var f_excise = parseFloat($("#f_excise").val())
				var f_total = parseFloat($("#f_total").val())
				var total = parseFloat($("#total").val())
				var h_excise = parseFloat($("#hid_excise").val())
				var h_tax = parseFloat($("#hid_tax").val())
		
				$("#hid_tax").val((tax_per).toFixed(2))
				$('#f_excise').val((excise).toFixed(2))
				$("#f_tax").val((f_tax-h_tax+tax_per).toFixed(2))
				$("#total").val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$("#f_total").val((f_total - total + final_amt).toFixed(2))
				$("#hid_excise").val(excise.toFixed(2))	
			}else{
				$('#total').val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$('#f_excise').val(excise.toFixed(2))
				$('#f_tax').val(tax_per.toFixed(2))
				$('#f_disc').val(disc_per.toFixed(2))
				$('#f_total').val(final_amt.toFixed(2))
				$("#hid_tax").val(tax_per.toFixed(2))
				$("#hid_disc").val(disc_per.toFixed(2))
				$("#hid_excise").val(excise.toFixed(2))		
			}
				
			}
			if(excise == '' && dis !=''){
					var tax_per,disc_per
			p=parseFloat(amt)
			t=parseFloat(tax)
			d=parseFloat(dis)
			disc_per = (p * d)/100
			var temp_amt = p - disc_per 
			tax_per= (temp_amt * t)/100
			to_tax= temp_amt + tax_per
			final_amt=to_tax
			excise = 0
			counter1=counter1+1
			if(counter1 >1){
		
				var f_tax=parseFloat($("#f_tax").val())	
				var f_total=parseFloat($("#f_total").val())
				var total=parseFloat($("#total").val())
				var h_tax=parseFloat($("#hid_tax").val())
		
				$("#hid_tax").val(tax_per.toFixed(2))
				$("#f_tax").val((f_tax-h_tax+tax_per).toFixed(2))
				$("#hid_excise").val(excise.toFixed(2))
				$("#f_excise").val(excise.toFixed(2))
				//alert(final_amt)
				$("#total").val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$("#f_total").val((f_total - total + final_amt).toFixed(2))	
			}else{
				$('#total').val(final_amt.toFixed(2))
				$('#f_tax').val(tax_per.toFixed(2))
				$('#f_disc').val(disc_per.toFixed(2))
				$('#f_total').val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$("#hid_tax").val(tax_per.toFixed(2))
				$("#hid_disc").val(disc_per.toFixed(2))
				$("#hid_excise").val(excise.toFixed(2))	
				$("#f_excise").val(excise.toFixed(2))		
			}
			}
			if(excise == '' && dis == ''){
				var tax_per,disc_per;
				excise = 0	
				p=parseFloat(amt),
				t=parseFloat(tax),
				tax_per= (p*t)/100,
				to_tax= p+tax_per,
				disc_per= 0,
				final_amt=to_tax ,
			counter1=counter1+1
			if(counter1 >1){
		
				var f_tax=parseFloat($("#f_tax").val())	
				var f_e=parseFloat($("#f_tax").val())	
				var f_total=parseFloat($("#f_total").val())
				var total=parseFloat($("#total").val())
				var h_tax=parseFloat($("#hid_tax").val())
		
				$("#hid_tax").val((tax_per).toFixed(2))
				$('#hid_excise').val((excise).toFixed(2))
				$('#f_excise').val((excise).toFixed(2))
				$("#f_tax").val((f_tax-h_tax+tax_per).toFixed(2))
				//alert(final_amt)
				$("#total").val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$("#f_total").val((f_total - total + final_amt).toFixed(2))	
			}else{
				$('#total').val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$('#f_tax').val(tax_per.toFixed(2))
				$('#f_disc').val(disc_per.toFixed(2))
				$('#f_total').val(final_amt.toFixed(2))
				$("#hid_tax").val(tax_per.toFixed(2))
				$("#hid_disc").val(disc_per.toFixed(2))
				$("#hid_excise").val(excise.toFixed(2))	
				$("#f_excise").val(excise.toFixed(2))		
			}	
			
				
				
			}
		
		}else{
			//alert("i am here")
			var tax_per,disc_per
			excise = parseFloat(excise)
			p = parseFloat(amt)
			t = parseFloat(tax)
			d = parseFloat(dis)
			disc_per = (p * d)/100
			var temp_amt = p - disc_per + excise 
			tax_per= (temp_amt * t)/100
			to_tax= temp_amt + tax_per
			final_amt=to_tax
			counter1=counter1+1
			if(counter1 >1){
				var f_tax = parseFloat($("#f_tax").val())	
				var f_excise = parseFloat($("#f_excise").val())	
				var f_total = parseFloat($("#f_total").val())
				var total=parseFloat($("#total").val())
				var h_tax=parseFloat($("#hid_tax").val())
				var h_excise = parseFloat($("#hid_excise").val())
				$("#hid_tax").val(tax_per.toFixed(2))
				$("#f_tax").val((f_tax-h_tax+tax_per).toFixed(2))
				$("#f_excise").val((f_excise - h_excise + excise ).toFixed(2))
				//alert(final_amt)
				$("#total").val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$("#f_total").val((f_total - total + final_amt).toFixed(2))	
			}else{
				$('#total').val(final_amt.toFixed(2))
				$('#f_tax').val(tax_per.toFixed(2))
				$('#f_disc').val(disc_per.toFixed(2))
				$('#f_total').val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$("#hid_tax").val(tax_per.toFixed(2))
				$("#hid_disc").val(disc_per.toFixed(2))		
				$('#hid_excise').val(excise.toFixed(2))
				$('#f_excise').val(excise.toFixed(2))
			}		
		}
		}else{
		alert('Select a Tax!')	
		
		}

	
	}




	function count_distotal(dis){
		if($('#total').val()!=''){
		var d=parseFloat(dis)
		var f_total = parseFloat($('#f_total').val())	
		var c_total = parseFloat($('#total').val())	
		var c_amt = parseFloat($('#amt').val())
		var tax = parseFloat($('#tax').val())
		if($('#excise').val() != ''){
			var excise = parseFloat($('#excise').val())	
		}else{
			
			var excise = 0
		}
			
		if(dis==''){
					var disc_per = 0	
		}else{
		var disc_per = (c_amt * d)/100				
		}
		var temp_amt = c_amt - disc_per + excise
 		var h_disc = parseFloat($('#hid_disc').val())	
		var f_disc = parseFloat($('#f_disc').val())
		var h_tax = parseFloat($('#hid_tax').val())	
		var f_tax = parseFloat($('#f_tax').val())
		var tax_per = (temp_amt * tax)/100
		//$('#hid_amt').val(c_amt)
		$('#f_disc').val((f_disc - h_disc + disc_per ).toFixed(2))
		$('#hid_disc').val(disc_per.toFixed(2))
		$('#f_tax').val((f_tax - h_tax + tax_per ).toFixed(2))
		$('#hid_tax').val(tax_per.toFixed(2))
		$('#total').val((c_total + h_disc - disc_per - h_tax + tax_per).toFixed(2))
		$("#hid_total").val((c_total + h_disc - disc_per - h_tax + tax_per).toFixed(2))
		$('#f_total').val((f_total + h_disc - disc_per - h_tax + tax_per).toFixed(2))
		$('#hid_excise').val(excise)
		}
	}


		function count_excise(excise){
		if($('#total').val()!=''){
		if($('#discount').val() != ''){
			var dis = parseFloat($('#discount').val())	
		}else{
			var dis = 0
			
		}
		var f_total = parseFloat($('#f_total').val())	
		var c_total=parseFloat($('#total').val())	
		var c_amt=parseFloat($('#amt').val())
		var tax=parseFloat($('#tax').val())	
		if(dis == 0){
					var disc_per = 0	
		}else{
		var disc_per = (c_amt * d)/100				
		}
		if(excise == ''){
			
			excise = 0
		}else{
			
			excise = parseFloat(excise)
		}
		var temp_amt = c_amt - disc_per + excise
 		var h_disc = parseFloat($('#hid_disc').val())	
		var f_disc = parseFloat($('#f_disc').val())
		var h_tax = parseFloat($('#hid_tax').val())
		var h_excise = parseFloat($('#hid_excise').val())	
		var f_tax = parseFloat($('#f_tax').val())
		var tax_per = (temp_amt * tax)/100
		//$('#hid_amt').val(c_amt)
		$('#f_disc').val((f_disc - h_disc + disc_per ).toFixed(2))
		$('#hid_disc').val(disc_per.toFixed(2))
		$('#hid_excise').val(excise)
		$('#f_excise').val(excise)
		$('#f_tax').val((f_tax - h_tax + tax_per ).toFixed(2))
		$('#hid_tax').val(tax_per.toFixed(2))
		$('#total').val((c_total + h_disc - disc_per - h_tax + tax_per - h_excise + excise).toFixed(2))
	$("#hid_total").val((c_total + h_disc - disc_per - h_tax + tax_per - h_excise + excise).toFixed(2))
	$('#f_total').val((f_total + h_disc - disc_per - h_tax + tax_per - h_excise + excise ).toFixed(2))
		
		}
	}



/*function count_amount1(qty,rate,x){
	var f_amt=parseFloat($('#f_amt').val())
	var id="#"+$(rate).attr('id')
	var r,
	r=$(id).val();
	n=parseInt(qty),
	p=parseFloat(r)
	var t='#'+'amt'+x
	$(t).val(n*p)
	var flag=n*p
	$('#f_amt').val(f_amt+flag)
	
	}*/

var counter=0
function count_total1(tax, x){
	var a='#'+'amt'+x
	if($(a).val()==''){
		return false	
	}
	if(tax != ''){
	var f_total = parseFloat($('#f_total').val())
	var f_tax = parseFloat($('#f_tax').val())
	
	var f_disc=parseFloat($('#f_disc').val())
	var tax_per,disc_per;
	var a='#'+'amt'+x
	var d='#'+'discount'+x
	var total='#'+'total'+x
	var hid_tax='#'+'hid_tax'+x
	var hid_disc='#'+'hid_disc'+x
	var hid_discount=parseFloat($(hid_disc).val())
	var hid_total='#'+'hid_total'+x
	
	p=parseFloat($(a).val())
	if($(d).val()!=''){
			d=parseFloat($(d).val())
			disc_per= (p*d)/100
			$(hid_disc).val(disc_per)

	}else{
		disc_per=0;
		$(hid_disc).val(disc_per)

	}
	var temp_amt = p - disc_per 
	t=parseFloat(tax)
	tax_per= (temp_amt * t)/100
	to_tax= temp_amt + tax_per
	final_amt=to_tax
	counter=counter+1
	if($(hid_tax).val()!=''){ 
		var h_tax=parseFloat($(hid_tax).val())
		var hidden_total=parseFloat($(hid_total).val())
		var h_total=parseFloat($(total).val())
		
		
		$(total).val(final_amt.toFixed(2))
		$(hid_tax).val((tax_per).toFixed(2))
		$("#f_total").val((f_total-hidden_total+final_amt).toFixed(2))	
		$(hid_total).val(final_amt.toFixed(2))
		$("#f_tax").val((f_tax - h_tax + tax_per).toFixed(2))
	    	$("#f_disc").val((f_disc + disc_per-hid_discount).toFixed(2))
	   
		
		
	}else{
			$("#f_total").val((f_total+final_amt).toFixed(2))
			$(total).val(final_amt.toFixed(2))
			$(hid_total).val(final_amt.toFixed(2))
			$("#f_tax").val((f_tax + tax_per).toFixed(2))
			$("#f_disc").val((f_disc + disc_per-hid_discount).toFixed(2))
			$(hid_tax).val(tax_per.toFixed(2))
			$(hid_disc).val(disc_per.toFixed(2))
		}
		}else{
			alert("Select A Tax!!")
		}
		
	}


	function count_amount1(qty,x){
		var q ='#'+'hid_qty'+x
		var n_no = parseInt(qty)
		var no = parseInt($(q).val())
		
		var c='#'+'code'+x
		var code = $(c).val()
	if(qty==''){
		var quantity='#'+'quantity'+x
		alert('quantity Filed Should not be Empty!!')
		if($(q).val()==''){
		$(quantity).focus()
		return false
		}else{
		$(quantity).val($(q).val())
				
		}
	}else{
		if(n_no > no ){
			var dif_no = n_no - no
		$.post("<?= site_url('sales/sales/search_quantity')?>",{ code:code, qty:dif_no  }, function(msg){
			//	alert(msg)
				if(msg == 1){
		var total='#'+'total'+x
		
	if($(total).val()!=''){
			//alert('ok')
			var excise ='#'+'excise'+x
			var r='#'+'rate'+x
			var p_a ='#'+'hid_amt'+x
			var p_d='#'+'hid_disc'+x
			var p_excise ='#'+'hid_excise'+x
			var excise ='#'+'excise'+x
			
			var p_t ='#'+'hid_tax'+x
			var r='#'+'rate'+x
			var c_amt='#'+'amt'+x
			var c_total='#'+'total'+x
			var discount='#'+'discount'+x
			var hid_total='#'+'hid_total'+x
			var tax='#'+'tax'+x
		if($(excise).val()==''){
			var ex = 0 
		}else{
			var ex = parseFloat($(excise).val())
		}
		var pr_n = parseFloat($(q).val())
		var n=parseInt(qty)
		var p=parseFloat($(r).val())
		
		var pr_disc = parseFloat($(p_d).val())
		var pr_tax = parseFloat($(p_t).val())
		var f_amt = parseFloat($('#f_amt').val())
		var f_tax = parseFloat($('#f_tax').val())
		var f_disc = parseFloat($('#f_disc').val())
		//alert(temp_tax)
	//	alert(temp_disc)
	
		var pr_amt= p * pr_n
		var new_amt= p * n
		$(c_amt).val(new_amt.toFixed(2))
		$(p_a).val(new_amt.toFixed(2))
		$('#f_amt').val((f_amt - pr_amt + new_amt).toFixed(2))
		$(q).val(n)	
		var discount = parseFloat($(discount).val())
		var tax = parseFloat($(tax).val())
		var disc_per = (new_amt * discount)/100
		var temp_amt = new_amt - disc_per + ex
		var tax_per = (temp_amt * tax)/100
		var to_tax= temp_amt + tax_per
		//var disc_per= (new_amt*discount)/100
		if(f_tax==0 && f_disc==0){
			var temp_tax = tax_per
			var temp_tax = disc_per
		}else{
				var temp_tax = f_tax - pr_tax + tax_per 
				var temp_disc = f_disc - pr_disc + disc_per
		}
		
		$(p_t).val(tax_per.toFixed(2))
		$(p_d).val(disc_per.toFixed(2))
		$('#f_tax').val(temp_tax.toFixed(2))
		$('#f_disc').val(temp_disc.toFixed(2))
		var f_total = parseFloat($('#f_total').val())
		var total = parseFloat($(total).val())
		var	n_total= to_tax
		var fn_total= f_total - total + n_total
		//alert(f_total)
		//alert(total)
		//alert(n_total)
		//alert(fn_total)
		$(c_total).val(n_total.toFixed(2))
		$(hid_total).val(n_total.toFixed(2))
		$('#f_total').val(fn_total.toFixed(2))
		
	}else{
	var f_amt=parseFloat($('#f_amt').val())
	var r='#'+'rate'+x
	var hid_amt='#'+'hid_amt'+x
	r=$(r).val();
	n=parseInt(qty),
	p=parseFloat(r)
	var t='#'+'amt'+x
	var hid_qty='#'+'hid_qty'+x
	$(hid_qty).val(qty)
	$(t).val((n*p).toFixed(2))
	var flag=n*p
	$(hid_amt).val((n*p).toFixed(2))
	$('#f_amt').val((f_amt+flag).toFixed(2))
	}
	}else if(msg==2){
			alert('Required Quiantity is not Available At Your Branch!!')
					var q ='#'+'hid_qty'+x
					var quantity ='#'+'quantity'+x
					$(quantity).val($(q).val())
				//	$('#quantity').focus()
				}else{
					alert('Required Quiantity is not Available On Stock!!')
						var q ='#'+'hid_qty'+x
					var quantity ='#'+'quantity'+x
					$(quantity).val($(q).val())
					//$('#quantity').focus()
				
				}
				
			});
	
	}else{
				var total='#'+'total'+x
		
	if($(total).val()!=''){
			//alert('ok')
			var excise ='#'+'excise'+x
			var r='#'+'rate'+x
			var p_a ='#'+'hid_amt'+x
			var p_d='#'+'hid_disc'+x
			var p_excise ='#'+'hid_excise'+x
			var excise ='#'+'excise'+x
			
			var p_t ='#'+'hid_tax'+x
			var r='#'+'rate'+x
			var c_amt='#'+'amt'+x
			var c_total='#'+'total'+x
			var discount='#'+'discount'+x
			var hid_total='#'+'hid_total'+x
			var tax='#'+'tax'+x
		if($(excise).val()==''){
			var ex = 0 
		}else{
			var ex = parseFloat($(excise).val())
		}
		var pr_n = parseFloat($(q).val())
		var n=parseInt(qty)
		var p=parseFloat($(r).val())
		
		var pr_disc = parseFloat($(p_d).val())
		var pr_tax = parseFloat($(p_t).val())
		var f_amt = parseFloat($('#f_amt').val())
		var f_tax = parseFloat($('#f_tax').val())
		var f_disc = parseFloat($('#f_disc').val())
		//alert(temp_tax)
	//	alert(temp_disc)
	
		var pr_amt= p * pr_n
		var new_amt= p * n
		$(c_amt).val(new_amt.toFixed(2))
		$(p_a).val(new_amt.toFixed(2))
		$('#f_amt').val((f_amt - pr_amt + new_amt).toFixed(2))
		$(q).val(n)	
		var discount = parseFloat($(discount).val())
		var tax = parseFloat($(tax).val())
		var disc_per = (new_amt * discount)/100
		var temp_amt = new_amt - disc_per + ex
		var tax_per = (temp_amt * tax)/100
		var to_tax= temp_amt + tax_per
		//var disc_per= (new_amt*discount)/100
		if(f_tax==0 && f_disc==0){
			var temp_tax = tax_per
			var temp_tax = disc_per
		}else{
				var temp_tax = f_tax - pr_tax + tax_per 
				var temp_disc = f_disc - pr_disc + disc_per
		}
		
		$(p_t).val(tax_per.toFixed(2))
		$(p_d).val(disc_per.toFixed(2))
		$('#f_tax').val(temp_tax.toFixed(2))
		$('#f_disc').val(temp_disc.toFixed(2))
		var f_total = parseFloat($('#f_total').val())
		var total = parseFloat($(total).val())
		var	n_total= to_tax
		var fn_total= f_total - total + n_total
		//alert(f_total)
		//alert(total)
		//alert(n_total)
		//alert(fn_total)
		$(c_total).val(n_total.toFixed(2))
		$(hid_total).val(n_total.toFixed(2))
		$('#f_total').val(fn_total.toFixed(2))
		
	}else{
	var f_amt=parseFloat($('#f_amt').val())
	var r='#'+'rate'+x
	var hid_amt='#'+'hid_amt'+x
	r=$(r).val();
	n=parseInt(qty),
	p=parseFloat(r)
	var t='#'+'amt'+x
	var hid_qty='#'+'hid_qty'+x
	$(hid_qty).val(qty)
	$(t).val((n*p).toFixed(2))
	var flag=n*p
	$(hid_amt).val((n*p).toFixed(2))
	$('#f_amt').val((f_amt+flag).toFixed(2))
	}
		
	}	
	
	}

	
}



	function count_distotal1(dis,x){ 
		var total='#'+'total'+x
		if($(total).val()!=''){
		
		var d=parseFloat(dis)
		var p_d = '#'+'hid_disc'+x
		var p_t ='#'+'hid_tax'+x
		var t ='#'+'tax'+x
		var hid_total='#'+'hid_total'+x
		var p_total='#'+'total'+x
		var amt='#'+'amt'+x
		var f_total = parseFloat($('#f_total').val())	
		var c_total=parseFloat($(p_total).val())	
		var c_amt=parseFloat($(amt).val())	
		if(dis==''){
			var disc_per = 0	
		}else{
			
		var disc_per = (c_amt * d)/100		
		}
		var temp_amt = c_amt - disc_per 
		alert(temp_amt); 
		var h_disc = parseFloat($(p_d).val())	
		var f_disc = parseFloat($('#f_disc').val())
		var tax =  parseFloat($(t).val())
		var h_tax = parseFloat($(p_t).val())	
		var f_tax = parseFloat($('#f_tax').val())
		var  tax_per = (temp_amt * tax)/100	   
		$('#f_disc').val((f_disc - h_disc + disc_per ).toFixed(2))
		$('#f_tax').val((f_tax - h_tax + tax_per ).toFixed(2))
		$(p_d).val(disc_per.toFixed(2))
		$(p_t).val(tax_per.toFixed(2))
		$(total).val((c_total + h_disc - disc_per - h_tax + tax_per ).toFixed(2))
		$(hid_total).val((c_total + h_disc - disc_per - h_tax + tax_per).toFixed(2))
		$('#f_total').val((f_total + h_disc - disc_per - h_tax + tax_per).toFixed(2))
		}
	}
	
	
	
	
	
	
	
	function count_all(rate){
		if($('#quantity').val()!='' && $('#total').val()!='' ){
			var r=parseFloat(rate)
			var qty=parseFloat($('#quantity').val())
			$('#amt').val((r*qty).toFixed(2))
			var final_amt=parseFloat($('#f_amt').val())
			var pre_amt=parseFloat($('#hid_amt').val())
			var amt = parseFloat((r*qty).toFixed(2))
			$('#f_amt').val((final_amt- pre_amt+ amt).toFixed(2))			
			var final_tax=parseFloat($('#f_tax').val())
			var final_disc=parseFloat($('#f_disc').val())
			var pre_tax=parseFloat($('#hid_tax').val())
			var pre_disc=parseFloat($('#hid_disc').val())			
			//alert($('#total').val())
			p=parseFloat(amt)
			t=parseFloat($('#tax').val())
			d=parseFloat($('#discount').val())
			disc_per =  (p*d)/100
			var temp_amt = p - disc_per 
			tax_per= (temp_amt * t)/100
			var temp_tax = final_tax - pre_tax + tax_per
			//alert(temp_tax)
			var to_tax = temp_amt + tax_per
			var temp_disc = final_disc - pre_disc + disc_per
			$('#f_tax').val(temp_tax.toFixed(2))
			$('#f_disc').val(temp_disc.toFixed(2))
			//alert(temp_disc)
			var temp_total = to_tax  
			$('#hid_tax').val(tax_per.toFixed(2))
			$('#hid_disc').val(disc_per.toFixed(2))
			$('#total').val(temp_total.toFixed(2))
			var pre_total = parseFloat($('#hid_total').val())
			var pre_final =parseFloat($('#f_total').val())
			//alert(pre_final)
			//alert(pre_total)
			//alert(temp_total)
			//alert(pre_final - pre_total + temp_total)
			$('#f_total').val((pre_final - pre_total + temp_total).toFixed(2))
			$('#hid_total').val(temp_total.toFixed(2))
		}else if($('#quantity').val()!=''){
			var r=parseFloat(rate)
			var qty=parseFloat($('#quantity').val())
			$('#amt').val((r*qty).toFixed(2))
			$('#hid_amt').val((r*qty).toFixed(2))
			$('#f_amt').val((r*qty).toFixed(2))
					
	
		}

	}
	
	
	
	
	function count_all1(rate,x){
				var quantity = '#'+'quantity'+x
				var total = '#'+'total'+x
				var p_amt = '#'+'amt'+x
				var hid_amt = '#'+'hid_amt'+x
				var hid_tax = '#'+'hid_tax'+x
				var hid_disc = '#'+'hid_disc'+x
				var tax = '#'+'tax'+x
				var discount = '#'+'discount'+x
				var hid_total = '#'+'hid_total'+x
				
		if($(quantity).val()!='' && $(total).val()!='' ){
			var r=parseFloat(rate)
			var qty=parseFloat($(quantity).val())
			var final_amt=parseFloat($('#f_amt').val())
			var pre_amt=parseFloat($(hid_amt).val())
			var amt = parseFloat(r*qty)
			//$(p_amt).val(amt)
			//alert(final_amt- pre_amt+ amt)
			$('#f_amt').val(final_amt- pre_amt+ amt)			
			var final_tax=parseFloat($('#f_tax').val())
			var final_disc=parseFloat($('#f_disc').val())
			var pre_tax=parseFloat($(hid_tax).val())
			var pre_disc=parseFloat($(hid_disc).val())			
			//alert($('#total').val())
			p=parseFloat(amt)
			t=parseFloat($(tax).val())
			d=parseFloat($(discount).val())
			disc_per = (p*d)/100
			var temp_disc= final_disc - pre_disc + disc_per
			var temp_amt = p - disc_per 
			tax_per= (temp_amt * t )/100
			var temp_tax = final_tax - pre_tax + tax_per
			//alert(temp_tax)
			var to_tax= temp_amt + tax_per
			$('#f_tax').val(temp_tax.toFixed(2))
			$('#f_disc').val(temp_disc.toFixed(2))
			$(p_amt).val(amt.toFixed(2))
			$(hid_amt).val(amt.toFixed(2))
			//alert(temp_disc)
			var temp_total = to_tax
			$(hid_tax).val(tax_per.toFixed(2))
			$(hid_disc).val(disc_per.toFixed(2))
			$(total).val(temp_total.toFixed(2))
			var pre_total = parseFloat($(hid_total).val())
			var pre_final =parseFloat($('#f_total').val())
			//alert(pre_final)
			//alert(pre_total)
			//alert(temp_total)
			//alert(pre_final - pre_total + temp_total)
			$('#f_total').val((pre_final - pre_total + temp_total).toFixed(2))
			$(hid_total).val(temp_total.toFixed(2))
		}else if($(quantity).val()!=''){
			var r=parseFloat(rate)
			var qty=parseFloat($(quantity).val())
			var final_amt=parseFloat($('#f_amt').val())
			var pre_amt=parseFloat($(hid_amt).val())
			var amt = parseFloat(r*qty)	
			//$(p_amt).val(r*qty)
			$(p_amt).val(amt.toFixed(2))
			$(hid_amt).val(amt.toFixed(2))
			$('#f_amt').val((final_amt- pre_amt+ amt).toFixed(2))
			
		}

	}


	var rount_counter = 0; 
function set_total(round_off){
	rount_counter = rount_counter + 1 
	var rd = "0"+"."+round_off
		var round = parseFloat(rd)
	if(rount_counter > 1){	
		var h_round = parseFloat($('#hid_round').val());
		var total = parseFloat($('#f_total').val());	
		$('#hid_round').val(round.toFixed(2))
		$('#f_total').val((total - h_round + round).toFixed(2))
		
	}else{
		var total = parseFloat($('#f_total').val())
		$('#hid_round').val(round.toFixed(2))
		$('#f_total').val((total + round).toFixed(2))

		}	
	}


	
	
	
function check(flag){
	$('#type_flag').val(flag);
}
function check_inv(flag){
	$('#flag_invoice').val(flag);
}

function form_valid(){
	
	var id_customer = parseInt($('#id_customer').val())
	var invoice_type =parseInt($('#flag_invoice').val())
	var transaction_type = parseInt($('#type_flag').val())
	var date = $('#date').val()
	if(date == ''){
		alert("Please Enter Date!")
		return false
	}
	if(id_customer == 0 && invoice_type== 0 ){
		alert('Select Customer!!')
		return false
	}
	
	if(id_customer == 0 && transaction_type== 1 ){
		alert('Select Customer!!')
		return false
	}


}	
	var trans_counter = 0;
function count_transtotal(trans_amt){
rount_counter = rount_counter + 1;
if(rount_counter > 1){

var prev_tr =  parseFloat($('#hid_trans').val())		
var tr_amt = parseFloat(trans_amt)
var total = parseFloat($('#f_total').val())
var trans_total = tr_amt + total - prev_tr
	
}else{

var tr_amt = parseFloat(trans_amt)
var total = parseFloat($('#f_total').val())
var trans_total = tr_amt + total
$('#hid_trans').val(tr_amt)		
}
$('#f_total').val(trans_total)	
	
	
}
function chk_status(y,x)
{
var quantity = '#'+'quantity'+x
var chk_status = '#'+'chk_quant'+x
var product_code='#'+'code'+x
var qty=parseFloat($(quantity).val())
var code=parseFloat($(product_code).val())

$.post("<?= site_url('enquiry/enquiry/search_quantity')?>",{ code:code, qty:qty  }, function(msg){
if(msg==1){ $(chk_status).html("Available")
}

else if(msg==2){
					alert('Required Quiantity is not Available At Your Branch!!')
		         	$(chk_status).html("Not Available at Branch")
				}else{
					alert('Required Quiantity is not Available On Stock!!')
					$(chk_status).html("Not Available on Stock")
				
				}


})
}


</script>
<?php $count=0;$total_amt=0;?>
 <?php foreach ($products as $product):?>
<?php $total_amt=$total_amt+($product->quantity*$product->selling_price);?>
<?php endforeach;?>
<!--<div id="infoMessage"><?php echo $message;?></div>-->
<a href="<?=site_url('enquiry/enquiry/view_enquiry')?>" class="btn btn-inverse"> Back </a>
<div class="span12">
<div class="span4">
<table align="center">
<tr><td><br>CUSTOMER</td><td>
            <?php 
	 	$bl[0] = "--Select Customer--";
	   		foreach($customers as $customer){
				$bl[$customer->id] = $customer->name;
			}
			echo form_dropdown('id_customer', $bl,$user->customer_id, 'class="chzn-select span12" data-placeholder="Choose a Customer" name="id_customer" id="id_customer" required style="width:220px;"');  ?>
          
	
<a href="<?=site_url('people/customer/create_customer/st')?>" ><i class="icon-edit"></i></a> 
</td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr>
<tr>
<td>ENQUIRY NO</td> <td><?php echo form_input($enquiry_no);?></td>
</tr>  
<tr>      
<td>ENQUIRY DATE</td><td><?php echo form_input($view_date);?></td>
</tr>
<tr>      
<td>EXPECTED DATE</td><td><?php echo form_input($expected_date);?>  </td>
</tr>
</table>
</div >
<div class="span3"></div>
<div class="span4">
<table>
<tr>
<td><b>Amount</b></td><td><input type="text" class="span2" name="total_amount" id="f_amt" value="<?= $total_amt?> " ></td>
</tr>
<tr>
<td><b>Total Discount</b></td><td><input type="text" name="total_discount" class="span2" id="f_disc" value=0  ></td>
</tr>

<td><b>Total Tax</b></td><td><input type="text" name="total_tax" class="span2" id="f_tax"  value=0></td>
</tr>
<tr>
<td><b>Round Off</b></td><td><input type="text" onchange="set_total();"  value="0."  readonly="readonly"  class="span1"><input type="text" onchange="set_total(this.value);" value="00" maxlength="2" name="round_off" class="span1" ></td>
</tr>
<tr>
<td colspan="2">--------------------------------------------------------------------------</td>
<tr>
<td><b>Total Amount</b></td><td><input type="text" name="total_price" class="span2" id="f_total" value="<?php echo $total_amt;?>" ></td>
</tr>

</tr>

</table>
</div>
</div>
<hr>

<hr>
<table width="100%" class="table table-bordered" >

  <tr class="info">
              <td><b>Item Code</b></td>
              <td><b>Rate</b></td>
              <td><b>Quantity</b></td>
              <td><b>Amount</b></td>
              <td><b>Discount(%)</b></td>
			  <td><b>Tax(%)</b></td>
              <td><b>Total</b></td>
			   <td><b>Status</b></td>
             </tr>
           
  <?php $count=0;$total_amt=0;?>
  <?php foreach ($products as $product):?>
  <?php $count=$count+1;
  ?>
		
<tr>
		<td style="border:none;"><input type="text" class="span2" value="<?= $product->code ?>" name='code[]'  id="code<?= $count ?>" /></td>
    <td style="border:none;"><input type="text" class="span2" value="<?= $product->selling_price ?>" id="rate<?= $count?>" onchange="count_all1(this.value, <?= $count ?>);"  name='rate[]' /><input type="hidden"   id="hid_rate<?= $count?>" name='hid_rate[]'/></td>
    <td style="border:none;"><input type="text" value="<?= $product->quantity ?>" class="span1" name="quantity[]" id="quantity<?= $count?>"  /><input type="hidden" id="hid_qty<?= $count?>" class="span8" name='hid_quantity[]'value="<?= $product->quantity ?>"><input type="hidden" id="prev_qty<?= $count?>" class="span8" name='prev_quantity[]'value="<?= $product->quantity ?>"></td>
    <td style="border:none;"><input type="text" class="span1" name="amount[]"  readonly="readonly" id="amt<?= $count?>" value="<?= $product->quantity*$product->selling_price ?>" /><input type="hidden" id="hid_amt<?= $count?>" class="span8" name='hid_amount[]' value="<?= $product->quantity*$product->selling_price ?>"  ></td>
    <td style="border:none;"><input type="text" class="span1" name="discount[]" id="discount<?= $count?>" onchange="count_distotal1(this.value, <?= $count ?>);" /><input type="hidden" value=0 id="hid_disc<?= $count?>" class="span8" name='hid_discount[]'   ></td>
	  
    <td style="border:none;" width="11%"><select class="we"  name="tax[]" id="tax<?= $count?>" class="chzn-select"  onchange="count_total1(this.value, <?= $count ?>);">
    <option value="0" >Select</option>
      <?php foreach($taxes as $tax) {?>
    <option value="<?= $tax->amount?>" 
    <?php 
	
	echo set_select('project',$tax->amount);

	?> 
     ><?= $tax->name?></option>
    <?php }?>
    </select>
    <input type="hidden" id="hid_tax<?= $count?>" name='hid_tax[]'  value=0 >
    </td>
    <td style="border:none;"><input type="text" class="span1" name="total[]" readonly="readonly" id="total<?= $count?>" value="<?= $product->quantity*$product->selling_price ?>" /> <input type="hidden" id="hid_total<?= $count?>" class="span8" name='hid_total[]' value="<?= $product->quantity*$product->selling_price ?>" ></td>
	<td  style="border:none;" id="status"width="11%"><a onclick="chk_status(this.value, <?= $count ?>);">Check Status</a><br><span id="chk_quant<?= $count?>">  </span> </td>

		</tr><?php $total_amt=$total_amt+($product->quantity*$product->selling_price);?>
		<?php endforeach;?>
  </tbody>
</table>
<div class="row-fluid">
<div class="span5">
</div>


<div class="span7">

</div>
</div>
<span align="right">
<form action="<?=site_url('enquiry/enquiry/sale_enquiry')?>" method="post">
<input type="hidden" name="hid_id" value="<?php echo $user->id;   ?>" >
<input type="hidden" name="hidden_enquiry" value="<?php echo $user->customer_enquiry_no; ?>">
<input type="submit"  value="Order" name="order" class="btn btn-info")>
</form>

</span>